package com.common.base.widget.tab

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import com.common.base.R
import com.common.base.widget.HeaderRecyclerView


/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：14/2/2023
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class MaxHeightRecyclerView : HeaderRecyclerView {
    private var mMaxHeight = 0

    constructor(context: Context) : super(context, null) {}
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        initialize(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initialize(context, attrs)
    }

    private fun initialize(context: Context, attrs: AttributeSet?) {
        val arr: TypedArray =
            context.obtainStyledAttributes(attrs, R.styleable.MaxHeightRecyclerView)
        mMaxHeight = arr.getLayoutDimension(R.styleable.MaxHeightRecyclerView_maxHeight, mMaxHeight)
        arr.recycle()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var heightMeasureSpecTemp = heightMeasureSpec
        if (mMaxHeight > 0) {
            heightMeasureSpecTemp = MeasureSpec.makeMeasureSpec(mMaxHeight, MeasureSpec.AT_MOST)
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpecTemp)
    }
}